<template>
	<view>
		<uni-row>
			<block>订阅成功后服务器会立即发送通知消息，一般在服务通知中,可自行查看手机微信</block>
		</uni-row>
		<uni-row>
			<button @click="onOnceClick">单次订阅</button>
		</uni-row>
	</view>
</template>

<script>
import { mapState } from 'vuex'
	
export default {
	data() {
		return {
			imgUrl: ''
		};
	},
	computed: {
		...mapState(['openid'])
	},
	methods: {
		onOnceClick() {
			uni.requestSubscribeMessage({
				tmplIds: ['G1eujs-JmHF7iAcmyj2emVNk4f9r3MFJtsJ9l8YoqD8'],
				success: (res) => {
					const key = 'G1eujs-JmHF7iAcmyj2emVNk4f9r3MFJtsJ9l8YoqD8';
					if (res[key] == 'accept') {
						uni.request({
							url: '自己服务器地址/subscribe',
							method: 'POST',
							data: {
								openid: this.openid
							},
							success: (res) => {
								if (res && res.data && res.data.code == 0) {
									uni.showToast({
										title: '订阅成功'
									});
								} else {
									uni.showToast({
										title: '订阅失败',
										icon: 'error'
									});
								}
							},
							fail: () => {
								uni.showToast({
									title: '订阅失败',
									icon: 'error'
								});
							}
						})
						
					} else {
						uni.showToast({
							title: '订阅失败',
							icon: 'error'
						});
					}
				},
				fail: (msg) => {
					console.log(msg)
				}
			})
		}
	}
};
</script>

<style lang="scss"></style>
